package com.golden.procedure.client.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.golden.procedure.activiti.client.activitiClientAPI;
import com.golden.procedure.client.IWorkOrderClientService;
import com.golden.procedure.client.ResultDO;
import com.golden.procedure.domain.WorkOrderDO;
import com.golden.procedure.domain.WorkOrderPickDO;
import com.golden.procedure.service.IWorkAttachmentService;
import com.golden.procedure.service.IWorkOrderPickService;
import com.golden.procedure.service.IWorkOrderService;
import com.golden.procedure.service.impl.BaseService;

@RestController
@RequestMapping("/api")
@Service("workOrderClientService")
public class WorkOrderClientServiceImpl extends BaseService implements IWorkOrderClientService {

	@Autowired
	activitiClientAPI clientAPI;

	@Autowired
	private IWorkOrderService workOrderService;
	@Autowired
	private IWorkOrderPickService workOrderPickService;
	@Autowired
	private IWorkAttachmentService workAttachmentService;

	/**
	 * 订单场次开启服务时，添加服务工单记录
	 * 
	 * @Method: create
	 * @Description:
	 * @param domain
	 * @return
	 * @see com.golden.procedure.client.IWorkOrderClientService#create(com.golden.procedure.domain.WorkOrderDO)
	 */
	@Transactional
	@PostMapping("/workOrder")
	public ResultDO create(@RequestBody WorkOrderDO domain) {
		ResultDO result = new ResultDO();

		com.golden.procedure.result.ResultDO resultDO = workOrderService.createWorkOrder(domain);

		if (!resultDO.isSuccess()) {
			result.setSuccess(false);
			result.setErrorCode(500);
			result.setErrorMsg(resultDO.getErrorMsg());
		}
		return result;
	}

	/**
	 * 更新加选订单状态及订单金额（提交订单、支付成功） <b>Description: 提交订单以及账单支付成功后，更新数据</b><br>
	 * 
	 * @param domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年10月12日 下午5:35:53 <br>
	 *       <b>Version:</b> 1.0
	 */
	@Transactional
	@PostMapping("/pickOrder")
	public ResultDO updatePickOrder(@RequestBody WorkOrderPickDO domain) {
		ResultDO result = new ResultDO();

		com.golden.procedure.result.ResultDO resultDO = workOrderPickService.updateAmount(domain);
		if (!resultDO.isSuccess()) {
			result.setSuccess(false);
			result.setErrorCode(500);
			result.setErrorMsg(resultDO.getErrorMsg());
		}
		return result;
	}

	/**
	 * 添加上传的文件 <b>Description: 添加上传的文件（单个文件添加）</b><br>
	 * 
	 * @param domain
	 * @return ResultDO
	 * @Note <b>Author:</b> Reading.Reagan <br>
	 *       <b>Date:</b> 2018年10月12日 下午5:35:53 <br>
	 *       <b>Version:</b> 1.0
	 */
	@PostMapping("/workPhoto")
	public boolean addPhoto(@RequestBody String params) {

		JSONObject jsonObject;
		Integer photographyID = null;
		Integer source = null;
		Integer fileID = null;
		String url = "";
		String businessNo = null;
		try {
			jsonObject = new JSONObject(params);
			photographyID = jsonObject.getInt("workPhotographyID");
			source = jsonObject.getInt("source");
			fileID = jsonObject.getInt("fileID");
			if (jsonObject.has("url")) {
				url = jsonObject.getString("url");
			}
			if (jsonObject.has("businessNo")) {
				businessNo = jsonObject.getString("businessNo");
				if (StringUtils.isEmpty(businessNo)) {
					businessNo = null;
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return false;
		}

		if (isNumberInValid(photographyID) || null == source || isNumberInValid(fileID) || StringUtils.isEmpty(url)) {
			return false;
		}
		com.golden.procedure.result.ResultDO resultDO = workAttachmentService.add(photographyID, source, fileID, url,
				businessNo);
		if (!resultDO.isSuccess()) {
			return false;
		}
		return true;
	}
}
